# pyexecjs
# tqdm
#
from urllib.parse import quote
import execjs
import time
import requests
import re
from tqdm import tqdm
js_code = execjs.compile(open('demo.js', encoding='utf-8').read())
headers = {
    "accept": "*/*",
    "accept-encoding": "gzip, deflate, br, zstd",
    "accept-language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
    "origin": "https://jx.xmflv.cc",
    "priority": "u=1, i",
    "sec-ch-ua": "\"Not)A;Brand\";v=\"8\", \"Chromium\";v=\"138\", \"Microsoft Edge\";v=\"138\"",
    "sec-ch-ua-mobile": "?0",
    "sec-ch-ua-platform": "\"Windows\"",
    "sec-fetch-dest": "empty",
    "sec-fetch-mode": "cors",
    "sec-fetch-site": "cross-site",
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36 Edg/138.0.0.0"
}
# v_url = 'https://v.qq.com/x/cover/mzc00200rujgpdm/i0045i5on24.html'
v_url = 'https://v.qq.com/x/page/z3356uw2rt3.html'
url = 'https://59.153.166.174:4433/xmflv.js'
now_time = int(time.time())
key = js_code.call('sign', now_time, quote(v_url))
data = {
    'wap': '0',
    'url': quote(v_url),
    'time': now_time,
    'key': key,
    'area': 'CNC|GuangDong_DongGuan-112.93.224.25'
}
response = requests.post(url, data=data, headers=headers)
json_data = response.json()
aes_key = json_data['aes_key']
aes_iv = json_data['aes_iv']
mw = json_data['url']
m3u8_url = js_code.call('decrypt', mw, aes_key, aes_iv)
print(m3u8_url)
m3u8 = requests.get(m3u8_url, headers=headers).text
ts_list = re.findall(',\n(.+?)\n#', m3u8)
i = 1
for ts in tqdm(ts_list):
    params = {
        'vkey':'6534646342516c57416c514641314a5741464a5a555656634467565656774e5756464548416c4d48426742514246774541564d41'
    }
    ts_content = requests.get(ts, headers=headers, params=params).content
    with open('视频.mp4', 'ab') as f:
        f.write(ts_content)
    # if i == 11:
    #     break
